﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

namespace Axe.Web.Controls
{
    public class DateSelector : CompositeControl, INamingContainer
    {
        DayMonthDownList Day = new DayMonthDownList();
        YearSelector Year = new YearSelector();
        MonthDropDownList Month = new MonthDropDownList();

        public DateSelector()
        {
            Day.ID = "Day";
            Month.ID = "Month";
            Year.ID = "Year";

            Year.LowYearOffset = -100;
            Year.HighYearOffset = 0;
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            Controls.Add(Day);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(Month);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(Year);

            ChildControlsCreated = true;
            ClearChildViewState();
        }

        public bool HasValidDate
        {
            get
            {
                try
                {
                    DateTime test = SelectedDate;
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        [Bindable(true, BindingDirection.TwoWay)]
        public DateTime SelectedDate
        {
            get
            {
                int day, month, year;
                
                day = int.Parse(Day.SelectedValue);
                month = int.Parse(Month.SelectedValue);
                year = Year.SelectedYear;

                return new DateTime(year, month, day);
            }
            set
            {
                Day.SelectedValue = value.Day.ToString();
                Month.SelectedValue = value.Month.ToString();
                Year.SelectedYear = value.Year;
            }
        }
    }

    public class DateTimeSelector : CompositeControl
    {
        DateSelector Date = new DateSelector();
        TimeSelector Time = new TimeSelector();

        public DateTimeSelector()
        {
            Date.ID = "Date";
            Time.ID = "Time";
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            Controls.Add(Time);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(Date);

            ChildControlsCreated = true;
            ClearChildViewState();
        }

        [Bindable(true, BindingDirection.TwoWay)]
        public DateTime SelectedDate
        {
            get
            {
                DateTime date = Date.SelectedDate;
                DateTime time = Time.SelectedTime;

                date = date.AddHours(time.Hour);
                date = date.AddMinutes(time.Minute);

                return date;
            }
            set
            {
                Date.SelectedDate = value;
                Time.SelectedTime = value;
            }
        }
    }
}
